<template>
    <div id="">
        <van-nav-bar title="修改联系人" left-text="返回" left-arrow @click-left="back" />
        <van-contact-edit is-edit :contact-info="editingContact" @save="onSave" @delete="onDelete" />
    </div>
</template>

<script>
export default {
    props: ["id"],
    components: {

    },
    data() {
        return {
            editingContact: {},
        }
    },
    methods: {
        back() {
            this.$router.back()
        },
        onSave(contactInfo) {
            // Toast('保存');
            console.log(contactInfo);
            this.$axios.put(this.$api.contactUpdate, contactInfo)
                .then(res => res.data)
                .then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$toast.success(res?.msg)
                        this.$router.push("/contactEdit")
                    } else {
                        this.$toast.fail(res?.msg)
                    }
                })
        },
        onDelete(contactInfo) {
            this.$axios.delete(`${this.$api.contactDelete}/${contactInfo.id}`)
                .then(res => res.data)
                .then(res => {
                    if (res.code == 200) {
                        this.$toast.success(res.msg)
                        this.$router.push("/contactEdit")
                    } else {
                        this.$toast.error(res.msg)
                    }
                })
        },
        init() {
            this.$axios.get(`${this.$api.contactGetById}/${this.id}`)
                .then(res => res.data)
                .then(res => {
                    if (res.code == 200) {
                        console.log(res.data);
                        this.editingContact = res.data[0]
                    }
                })
        }
    },
    mounted() {
        console.log(this.id);
        this.init()
    }
}
</script>

<style scoped></style>
